home *** CD-ROM | disk | FTP | other *** search
/ X User Tools / X User Tools (O'Reilly and Associates)(1994).ISO / sun4c / archive / timex.z / timex / slib / X11 / perl / weekno.perl
Text File  |  1994-09-27  |  2KB  |  82 lines

  1. #!///////////////////////////////////////////////////////////////////////////usr/STAGE/bin/perl
  2. # Package for date handling
  3. package DATE;
  4. # Use the PERL std library timelocal - older had his own
  5. require "timelocal.pl";
  6. #               J   F   M   A   M   J   J   A   S   O   N   D
  7. @daysinmonth = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  8. # Find timezone (now) by comparing gmtime to localtime
  9. # for a fixed time that is not on a month boundary
  10. $sometime = 150000;
  11. @gmtime = gmtime($sometime);
  12. @localtime = localtime($sometime);
  13. $timezone = $localtime[2]  - $gmtime[2];
  14. # Day changes.
  15. if ($localtime[3] == $gmtime[3]) {
  16.    # do nothing
  17. } elsif ($localtime[3] == $gmtime[3] + 1) {
  18.    $timezone += 24;
  19. } elsif ($localtime[3] == $gmtime[3] - 1) {
  20.    $timezone -= 24;
  21. } else {
  22.    $gm = join(":", @gmtime);
  23.    $lt = join(":", @localtime);
  24.    die "Problems with timezone: comparing $gm to $lt\n";
  25. }
  26.  
  27.  
  28.  
  29. sub leapyear {
  30.    local($y) = @_;
  31.    local($leap4, $leap100, $leapyear);
  32.    $leap4 = $y / 4;
  33.    $leap100 = $y / 100;
  34.    if (int($leap4) == $leap4 && int($leap100) != $leap100) {
  35.       $leapyear = 1;
  36.    }
  37.    $leapyear;
  38. }
  39.  
  40. sub weekno {
  41. # Given a time_tm, return week number
  42.    local($tm) = @_;
  43.    local($year, $weekno, $dummy);
  44.    ($dummy, $dummy, $dummy,
  45.     $dummy, $dummy, $year,
  46.     $dummy, $yday, $dummy) = localtime($tm);
  47.    $weekno = int(($yday - &firstdayfirstweek($year) + 7) / 7);
  48.    $weekno;
  49. }
  50.  
  51.  
  52. sub firstdayfirstweek {
  53. # Return first day of week 1 of any year
  54.    local($y) = @_;
  55.    local($ret);
  56.    # Get time of January 1, 0.0.0.0
  57.    # Note that DST is never in effect on Jan 1...
  58.    local($firsttime) = &timelocal(0, 0, 0, 1, 0, $y, 0, 0, 0);
  59.    local(@firstday) = localtime($firsttime);
  60.    local($wday) = $firstday[6];
  61.    # Rule works for some years.....89 to 92 tested, they all hit branch 2...
  62.    if ($wday > 3) {
  63.       $ret = 8 - $wday;
  64.    } else {
  65.       $ret = 1 - $wday;
  66.    }
  67.    $ret;
  68. }
  69.  
  70. sub firstinweek {
  71. # Return timevalue for 2 hours into the day
  72. # (to avoid DST troubles)
  73.    local($weekno, $year) = @_;
  74.    # 2 hours into this year
  75.    local($time) = &timelocal(0, 0, 2, 0, 0, $year, 0, 0, 0);
  76.    # Add number of days since start of year
  77.    $time += (($weekno - 1) * 7 + &firstdayfirstweek($year) + 1) * 60 * 60 * 24;
  78.    $time;
  79. }
  80.  
  81. 1; # Everything is fine
  82.